package com.example.casper.Experiment2024;

import static android.widget.Toast.LENGTH_LONG;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.casper.Experiment2024.data.DataBank;
import com.example.casper.Experiment2024.data.TravelRecord;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
import com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions;

import java.util.ArrayList;

public class TencentMapFragment extends Fragment {
    private TextureMapView mapView;
    private Button btnZoomIn;
    private Button btnZoomOut;
    private ArrayList<TravelRecord> tripRecords;
    private Marker marker;
    private LatLng point1;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = rootView.findViewById(R.id.mapview_tencent);

        // 初始化地图
        TencentMap tencentMap = mapView.getMap();

        loadTripRecords();
        point1 = new LatLng(22.249942, 113.534341);
        Log.d("initPoint", String.valueOf(point1));
        tencentMap.getUiSettings().setZoomGesturesEnabled(true);
        // 设置初始位置和缩放级别
        tencentMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point1, 13));


        for (TravelRecord record : tripRecords) {
            try {
                Log.d("Location_Debug", "Record Data: " + record.toString());
                double latitude = record.getLatitude();
                double longitude = record.getLongitude();
                LatLng point = new LatLng(latitude, longitude);
                Log.d("Location_la_get", Double.toString(latitude));

                // 创建 MarkerOptions
                MarkerOptions markerOptions = new MarkerOptions()
                        .position(point) // 设置 Marker 的位置
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.zuobiao1)) // 设置 Marker 的图像资源
                        .title(record.getPositionName()) // 设置 Marker 的标题
                        .snippet(record.getDescription()); // 设置 Marker 的详细信息

                // 添加 Marker 到地图上
                marker = tencentMap.addMarker(markerOptions);
                marker.setClickable(true);

            } catch (NumberFormatException e) {
                e.printStackTrace(); // 处理解析错误，例如坐标字段为空或非法
            }
        }


        tencentMap.setOnMarkerClickListener(new TencentMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(TencentMapFragment.this.getContext(), "你点击了腾讯地图",LENGTH_LONG).show();



                return true;
            }
        });
        marker.setClickable(true);

        btnZoomIn = rootView.findViewById(R.id.button_big);
        btnZoomOut = rootView.findViewById(R.id.button_short);

        // 为放大按钮设置点击事件
        btnZoomIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tencentMap.moveCamera(CameraUpdateFactory.zoomIn());
            }
        });

        // 为缩小按钮设置点击事件
        btnZoomOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tencentMap.moveCamera(CameraUpdateFactory.zoomOut());
            }
        });
        loadMap();

        return rootView;
    }

    private void loadTripRecords() {
        DataBank dataBank = new DataBank(getContext());
        tripRecords = dataBank.readTravelRecords(); // 从本地存储读取记录
        if (tripRecords.isEmpty()) {
            tripRecords = new ArrayList<>();
            tripRecords.add(new TravelRecord("暨南大学珠海校区", "学校", null, "2025-01-01", 22.251453, 113.53645));
            Log.d("TencentMapFragment", "DataBank中没有读取到记录信息，初始化空列表");
        }
    }

    private void loadMap() {
        TencentMap tencentMap = mapView.getMap();
        DataBank dataBank = new DataBank(getContext());
        ArrayList<TravelRecord> records = dataBank.readTravelRecords();

        // 用于绘制足迹的点
        PolylineOptions polylineOptions = new PolylineOptions();

        for (TravelRecord record : records) {
            LatLng position = new LatLng(record.getLatitude(), record.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions(position)
                    .position(position)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.zuobiao1))
                    .title(record.getPositionName())
                    .snippet(record.getDescription());
            Marker marker=tencentMap.addMarker(markerOptions);
            tencentMap.setOnMarkerClickListener(new TencentMap.OnMarkerClickListener(){
                @Override
                public boolean onMarkerClick(Marker marker) {
                    if(marker.getId().equals(marker.getId())) {
                        //自定义Marker被点击
                        Toast.makeText(TencentMapFragment.this.getContext(),record.getPositionName()+record.getDescription(),Toast.LENGTH_LONG).show();
                    }
                    return true;
                }
            });
            //设置Marker支持点击
            marker.setClickable(true);
            polylineOptions.add(position); // 添加到足迹
        }

        // 绘制虚线
        tencentMap.addPolyline(polylineOptions.color(0xFF0000FF).width(5));

    }



    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mapView != null) {
            mapView.onResume();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mapView != null) {
            mapView.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }

}
